package com.ketai.web;

import com.alibaba.fastjson.JSON;
import com.ketai.common.MessageConstant;
import com.ketai.common.Result;
import com.ketai.common.StatusCode;
import com.ketai.pojo.CUser;
import com.ketai.pojo.Consul;
import com.ketai.service.ConsulService;
import com.ketai.service.impl.ConsulServiceImpl;
import com.ketai.util.Pages;
import com.ketai.util.SendUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author:胡远博
 * @Description:
 * @Date:Create In 2022-08-11 11:31
 **/
@WebServlet("/consulServlet")
public class ConsulServlet extends HttpServlet {
    ConsulService consulService = new ConsulServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String opr = req.getParameter("opr");
        if ("list".equals(opr)) {
            Integer curr = Integer.parseInt(req.getParameter("page"));
            Integer limit = Integer.parseInt(req.getParameter("limit"));
            String start_time = req.getParameter("startTime");
            String end_time = req.getParameter("endTime");
            String title = req.getParameter("title");
            String type = req.getParameter("type");
            String checked = req.getParameter("checked");

            Pages<Consul> consulPages = consulService.getAllCUser(curr, limit);
            //调用工具类 返回数据给前端
            //SendUtil.SendPageJson(resp, ActivitiePages);
            //List<CUser> users = cUserService.getAllCUser(curr, limit);
            Map<String, Object> tableData = new HashMap<String, Object>();
            //这是layui要求返回的json数据格式
            tableData.put("code", 2000);
            tableData.put("msg", "");
            //将全部数据的条数作为count传给前台（一共多少条）
            tableData.put("count", consulPages.getDataTotal());
            //将分页后的数据返回（每页要显示的数据）
            tableData.put("data", consulPages.getDataItems());
            resp.getWriter().write(JSON.toJSONString(tableData));
        }else if("del".equals(opr)){
            String id = req.getParameter("id");
            int result = consulService.deleteById(id);
            Result r = Result.message(MessageConstant.CONSUL_DELETE_SUCCESS);
            if (result == 0) {
                r = Result.error(StatusCode.SQLERROR, MessageConstant.CONSUL_DELETE_ERROR);
            }
            SendUtil.Sendjson(resp, r);
        }else if("insert".equals(opr)){
            String title = req.getParameter("title");
            String detaic = req.getParameter("detaic");
            String time = req.getParameter("time");
            String created = req.getParameter("created");
            Consul consul = new Consul(null,title,detaic,time,created);
            consulService.insertConsul(consul);
            SendUtil.Sendjson(resp, Result.ok(MessageConstant.CONSUL_SAVE_SUCCESS, 100));
        }
    }
}
